package com.neusoft.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Orders;
import com.neusoft.service.OrdersService;

@RefreshScope
@RestController
@RequestMapping("/OrdersController")
public class OrdersController {
	@Autowired
	OrdersService ordersService;
	
	@PostMapping("/createOrders/{userId}/{businessId}/{daId}/{allprice}")
	public CommonResult<Integer> createOrders(
			@PathVariable("userId") String userId,
			@PathVariable("businessId") int businessId,
			@PathVariable("daId") int daId,
			@PathVariable("allprice") double orderTotal
			){
		Orders orders=new Orders();
		orders.setUserId(userId);
		orders.setBusinessId(businessId);
		orders.setDaId(daId);
		orders.setOrderTotal(orderTotal);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd　HH:mm:ss");
		String datestr=sdf.format(new Date());
		orders.setOrderDate(datestr);
		int s = -1;
		try {
			s = ordersService.createOrders(orders);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new CommonResult<Integer>(200,"success",s);
	}
	
	@GetMapping("/listOrdersByUserId/{userId}")
	public CommonResult<List<Orders>> listOrdersByUserId(
			@PathVariable("userId") String userId)
	{
		return new CommonResult<List<Orders>>(200,"success",ordersService.listOrdersByUserId(userId));
	}
	
	@GetMapping("/getOrdersById/{orderSn}")
	public CommonResult<Orders> getOrdersById(@PathVariable("orderSn")int orderSn){
		return new CommonResult<Orders>(200,"success",ordersService.getOrdersById(orderSn));
	}

	@PutMapping("/setOrderSuccess/{orderId}")
	public CommonResult<Integer> setOrderSuccess(@PathVariable("orderId") int orderId){
		return new CommonResult<Integer>(200,"success",ordersService.setOrderSuccess(orderId));
	}
}
